package xyz.mumuwei.youthcourseserver.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import xyz.mumuwei.youthcourseserver.common.lang.Result;
import xyz.mumuwei.youthcourseserver.pojo.CourseTime;
import xyz.mumuwei.youthcourseserver.service.CourseTimeService;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author mumuwei
 * @since 2022-02-19
 */
@RestController
@RequestMapping("/course/time")
public class CourseTimeController {
    private CourseTimeService timeService;

    @Autowired
    public void setTimeService(CourseTimeService timeService) {
        this.timeService = timeService;
    }

    /**
     * 根据 id 获取课程时间
     * @param id 课程时间 id
     */
    @GetMapping("/{id}")
    public Result getCourseTimeById(@PathVariable Integer id){
        CourseTime time = timeService.getById(id);
        if(time == null){
            return Result.fail("课程时间不存在！");
        }
        return Result.success(time);
    }

    /**
     * 添加一个上课时间
     * @param courseTime 上课时间对象
     */
    @PostMapping("")
    public Result add(@Validated CourseTime courseTime){
        List<CourseTime> list = timeService.list(new QueryWrapper<CourseTime>()
                .eq("course_id", courseTime.getCourseId())
                .eq("start_time", courseTime.getStartTime())
                .eq("end_time", courseTime.getEndTime()));
        if(list.size() != 0){
            return Result.fail("已存在同一时段上课信息");
        }
        boolean save = timeService.save(courseTime);
        if(save){
            return Result.success("添加成功");
        }
        return Result.fail("添加失败！");
    }

    /**
     * 根据id修改上课时间
     * @param courseTime 修改上课时间对象
     */
    @PostMapping("/update")
    public Result update(@Validated CourseTime courseTime){
        List<CourseTime> list = timeService.list(new QueryWrapper<CourseTime>()
                .eq("class_time", courseTime.getStartTime())
                .eq("end_time", courseTime.getEndTime())
                .eq("course_id", courseTime.getCourseId())
                .notIn("id", courseTime.getId()));
        if(list.size() > 0){
            return Result.fail("已存在同一时段上课信息");
        }
        boolean save = timeService.updateById(courseTime);
        if(save){
            return Result.success("修改成功");
        }
        return Result.fail("修改失败！");
    }

    /**
     * 根据id删除上课时间
     */
    @GetMapping("/del_{id}")
    public Result delete(@PathVariable @NotNull(message = "id不能为空") Integer id){
        if(deleteById(id)){
            return Result.success("删除成功!");
        }
        return Result.fail("删除失败!");
    }

    /**
     * 根据课程 id获取上课时间
     * @param id 课程 id
     */
    @GetMapping("/c_{id}")
    public Result getTimesByCourseId(@PathVariable Integer id){
        List<CourseTime> list = timeService.list(new QueryWrapper<CourseTime>()
                .eq("course_id", id));
        return Result.success(list);
    }

    /**
     * 根据 id 删除上课时间
     */
    private boolean deleteById(Integer id){
        return timeService.removeById(id);
    }
}

